<?php
class Warehouse_Model_TakeStockItem
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Warehouse_Model_DbTable_TakeStockItem();
	}
	
	/**
	*Create A Record  
	*@param rowSet $row warehouse's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto('tsi_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
	
	/**
	*Delete One Row
	*@return boolean
	*/
	public function deleteByField($field,$value){	
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value); 
		return $this->_table->delete($where);		
	}
	
	public function getByCond($condition=array(),$type='*',$pageSize=0,$page=1,$orderby=array())
	{
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition['ts_id']) && $condition['ts_id']!=''){
            $select->where('ts_id=?', $condition['ts_id']);
        }
		if(isset($condition['ts_code']) && $condition['ts_code']!=''){
            $select->where('ts_code=?', $condition['ts_code']);
        }
        if(isset($condition['tsi_stk_status']) && $condition['tsi_stk_status'] !== ''){
            $select->where('tsi_stk_status=?', $condition['tsi_stk_status']);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	/**
	 * 根据条件从take_stock_item表中读取一行
	 * @return 一维数组
	 */
	public function getRowByCond($condition=array(),$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("1 =?",1);
        if(isset($condition['ws_code']) && $condition['ws_code']!=''){
            $select->where('ws_code=?', $condition['ws_code']);
        }
		if(isset($condition['ts_code']) && $condition['ts_code']!=''){
            $select->where('ts_code=?', $condition['ts_code']);
        }
       
        $select->order($orderby);
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql);
	}
	
	public function getByfield($field,$value){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchRow($where);
	}
	
	/**
	 * 获取指定条件的全部数据
	 *
	 */
	public function getAllByCond($cond){
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("1 =?",1);
        if(isset($cond["ts_code"]) && $cond["ts_code"]!=''){
            $select->where( $table.".ts_code = ?",$cond["ts_code"]);
        }
		if(isset($cond["warehouse_id"]) && $cond["warehouse_id"]!=''){
            $select->where( $table.".warehouse_id = ?",$cond["warehouse_id"]);
        }
        $sql=$select->__toString();
		return $this->_table->getAdapter()->fetchAll($sql);		
	}
	
	public  function updateByCond($row,$cond){
		$adapter = $this->_table->getAdapter();
		$where = $adapter->quoteInto(" ts_code= ?", $cond['ts_code']);
		$where .= $adapter->quoteInto(" and product_id= ?", $cond['product_id']);
		return $this->_table->update($row, $where);
	}
	
}